{include file="public/head"/}
<div class="layui-fluid" id="LAY-app-message">
    <div class="layui-card">
        <div class="layui-card-header">友链列表</div>
        <div class="layui-card-body">
            <table class="layui-table" id="list" lay-filter="list"></table>
        </div>
    </div>
</div>
{include file="public/foot"/}
<script type="text/html" id="url">
    <a href="{{d.link}}" target="_blank">{{d.link}}</a>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加友链</button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="direct"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
    </div>
</script>
<script>
    layui.config({
        base: '__LAYDMIN__/',{if config('app.app_debug')}version: true{/if}
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','admin'],function(){

		var $ = layui.$;
		var admin = layui.admin;
		var table = layui.table;
		var element = layui.element;
		var form = layui.form;
		var util = layui.util;
		var upload = layui.upload;
		var carousel = layui.carousel;
		var device = layui.device();
		var laydate = layui.laydate;
		var dropdown=layui.dropdown;
		var tableLoadDiv = window.top.layer.load(0,{shade:[0.5, '#000']}); //加载层,table.render完成时需要关闭
		//全部数据
		tableIn = table.render({
			elem: '#list',
			url: '{:url(SYS_PATH."/".CONTROLLER."/index")}',
			toolbar: '#toolbar',
			method: 'post',
			cols: [[
				{checkbox: true, fixed: true},
				{field: 'id',align: 'center', title: 'ID', width: 80, fixed: true, sort: true},
				{field: 'title', title: '链接名称', width: 200},
				{field: 'link', title: '链接URL', width: 300,templet:'#url'},
				{field: 'sort',align: 'center',title: '排序', width: 80, sort: true,edit:'text'},
				{field: 'status',align: 'center', title: '状态', width: 100, sort: true,toolbar: '#status'},
				{field: 'create_time', title: '添加时间', width: 180,sort:true,align:'center',
					templet: function(d){
					return util.toDateString(d.create_time*1000,'yyyy-MM-dd HH:mm');
					}
				},
				{width: 160, align: 'center', toolbar: '#action'}
			]],
			done:function (res) {   //返回数据执行回调函数
				window.top.layer.close(tableLoadDiv);    //返回数据关闭loading
			}				
		});
		//改变友链状态
		form.on('switch(status)', function (obj) {
			var id = this.value;
			var status = obj.elem.checked === true ? 1 : 0;
			admin.req({
				url: '{:url(SYS_PATH."/".CONTROLLER."/doStatus")}',
				type:'post',
				data: {'id': id, 'status': status},
				success: function (res) {
					if (res.code== 0) {
						window.top.layer.msg(res.msg, {time: 1000, icon: 1,shade:[0.5,'#000']});

					} else {
						window.top.layer.msg(res.msg, {time: 1000, icon: 2,shade:[0.5,'#000']});
						if (obj.elem.checked == true) {
							$(obj.elem).prop('checked', false);
						} else {
							$(obj.elem).prop('checked', true);
						}
						form.render();
					}
				}
			});
		});
	
	
		//监听工具条
		table.on('tool(list)', function (obj) {
			var data = obj.data;
			var id = data.id;
			if (obj.event === 'del') {
				var uri="{:url(SYS_PATH.'/'.CONTROLLER.'/doDelete')}"
				layui.doDelete(uri,"确定要删除吗？",id,obj);//单个删除接口				
			}else if (obj.event === 'edit') {
				layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/edit')}?id="+id,data.title);
			}
		});
		
		//头工具栏事件
		table.on('toolbar(list)', function(obj){
			switch(obj.event){
				case 'add':
					layui.layerOpen("{:url(SYS_PATH.'/'.CONTROLLER.'/add')}","添加友链");
					break;
				case 'direct':
					layui.deleteAll("{:url(SYS_PATH.'/'.CONTROLLER.'/doDeleteAll')}","确定要批量删除吗？",tableIn);//tableIn为渲染列表对象
					break;
			}
		});		
		
	});
</script>
</body>
</html>